package cn.tedu.crm.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.tedu.crm.base.exception.ServiceException;
import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import cn.tedu.crm.role.mapper.RoleMapper;
import cn.tedu.crm.role.mapper.RolePermissionMapper;
import cn.tedu.crm.role.pojo.dto.RoleQuery;
import cn.tedu.crm.role.pojo.dto.RoleSaveParam;
import cn.tedu.crm.role.pojo.entity.Role;
import cn.tedu.crm.role.pojo.entity.RolePermission;
import cn.tedu.crm.role.pojo.vo.RoleVO;
import cn.tedu.crm.role.pojo.vo.RoleSelectVO;
import cn.tedu.crm.role.pojo.vo.RoleVO;
import cn.tedu.crm.role.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tedu
 * @description 针对表【role(角色表)】的数据库操作Service实现
 * @createDate 2025-03-11 16:07:45
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public List<RoleVO> selectRole(RoleQuery roleQuery) {
        /*QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(roleQuery.getName()),"name",roleQuery.getName());
        List<Role> list = roleMapper.selectList(queryWrapper);
        List<RoleVO> roleVOS = new ArrayList<>();
        for (Role role : list) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role,roleVO);
            roleVOS.add(roleVO);
        }*/
        List<RoleVO> list = roleMapper.selectRoleList(roleQuery);
        return list;
    }

    @Override
    @Loggable(module = OperationModule.ROLE_AUTHORITY,type = OperationType.DELETE)
    public void deleteRole(Long id) {
        log.debug("删除角色：{}", id);
        roleMapper.deleteById(id);
        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id", id));
    }

    @Transactional
    @Override
    @Loggable(module = OperationModule.ROLE_AUTHORITY,type = OperationType.INSERT_UPDATE)
    public void saveRole(RoleSaveParam roleSaveParam) {
        Role role = new Role();
        BeanUtils.copyProperties(roleSaveParam, role);
        if (role.getId() == null) {
            role.setCreateTime(new Date());
            role.setStatus(1);
            roleMapper.insert(role);
        } else {
            role.setUpdateTime(new Date());
            roleMapper.updateById(role);
            rolePermissionMapper.deleteByRoleId(role.getId());
        }
        for (long menuId : roleSaveParam.getMenuIds()) {
        RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(menuId);
            rolePermissionMapper.insert(rolePermission);
        }
    }

    @Override
    public RoleSelectVO selectRoleById(Long id) {
        // 1.
        Role role = roleMapper.selectById(id);
        if (role == null) {
            throw new ServiceException("角色不存在");
        }
        RoleSelectVO roleSelectVO = new RoleSelectVO();
        BeanUtils.copyProperties(role, roleSelectVO);

        // 2.
        List<Long> permissionIds = rolePermissionMapper.selectPermissionIdsByRoleId(id);
        roleSelectVO.setMenuIds(permissionIds);

        return roleSelectVO;
    }

    @Override
    public List<RoleVO> selectAllRole() {
        List<Role> list = roleMapper.selectList(null);
        return list.stream()
                .map(role -> BeanUtil.copyProperties(role, RoleVO.class))
                .collect(Collectors.toList());
    }
}
